﻿using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System;
using System.Web;

namespace TicketAppDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            login();
            Console.ReadLine();
        }
        public static void login()
        {
            const string loginUrl = "https://frontier.beij.12306.cn/gateway/hydzsw/Dzsw/j_spring_security_check";
            const string userName = "3029233";
            const string password = "0112";
            const string postData = "j_username=3029233&j_password=1111&j_captcha=nazac&fromUrl=%2Flogin_bur.jsp";
            Encoding encoding = Encoding.GetEncoding("gb2312");

            IDictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("tpl", "fa");
            parameters.Add("tpl_reg", "fa");
            parameters.Add("psp_tt", "0");
            parameters.Add("username", userName);
            parameters.Add("password", password);
            parameters.Add("mem_pass", "1");
            HttpWebResponse response = Common.CreatePostHttpResponse(loginUrl, parameters, null, null, encoding, null);
            string cookieString = response.Headers["Set-Cookie"]; 
        }

        public string SendRequestToServer(bool isCompressed)
        {
            string postData = "";// this.PostString;
            string response = string.Empty;
            string url = "https://blablabla";
            HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            req.AllowWriteStreamBuffering = true;
            req.Method = "POST";
            req.ContentType = "text/xml";
            string frmData = HttpUtility.UrlEncode(postData);
            frmData = "xmldata=" + frmData;
            byte[] formData = UTF8Encoding.UTF8.GetBytes(frmData);
            req.ContentLength = formData.Length;

            using (Stream post = req.GetRequestStream())
            {
                post.Write(formData, 0, formData.Length);
                post.Close();
            }

            using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(resp.GetResponseStream());
                string resContent = string.Empty;
                resContent = reader.ReadToEnd();
                response = resContent;
            }
            return response;
        }
        public static string PostDataGetHtml(string uri, string postData)
        {
            try
            {
                byte[] data = Encoding.UTF8.GetBytes(postData);

                Uri uRI = new Uri(uri);
                HttpWebRequest req = WebRequest.Create(uRI) as HttpWebRequest;
                req.Method = "POST";
                req.KeepAlive = true;
                req.ContentType = "application/x-www-form-urlencoded";
                req.UserAgent =
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36";
                req.Referer = "https://frontier.beij.12306.cn/gateway/hydzsw/Dzsw/login_bur.jsp";
                
                req.ContentLength = data.Length;
                req.AllowAutoRedirect = true;

                Stream outStream = req.GetRequestStream();
                outStream.Write(data, 0, data.Length);
                outStream.Close();

                HttpWebResponse res = req.GetResponse() as HttpWebResponse;
                Stream inStream = res.GetResponseStream();
                StreamReader sr = new StreamReader(inStream, Encoding.UTF8);
                string htmlResult = sr.ReadToEnd();

                return htmlResult;
            }
            catch (Exception ex)
            {
                return "网络错误：" + ex.Message.ToString();
            }
        }
    }
}
